package com.example.ljw.frescowallpaper.view;

import android.content.Context;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.ljw.frescowallpaper.R;
import com.example.ljw.frescowallpaper.adapter.MyDownloadAdapter;
import com.example.ljw.frescowallpaper.adapter.MyRecyclerAdapter;
import com.example.ljw.frescowallpaper.base.BaseActivity;
import com.example.ljw.frescowallpaper.helper.SDCardHelper;
import com.example.ljw.frescowallpaper.widget.DividerItemDecoration;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyDownloadActivity extends BaseActivity {

    //初始化上下文
    private Context mContext = this;
    //初始化recyclerview
    private RecyclerView mydownload_recyclerview;
    //初始化适配器
    private MyDownloadAdapter mAdapter;
    //download文件夹下的文件
    private List<File> mDownloadFiles;

    private ImageView imageview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_download);

//        initData();
//        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
        initView();
    }

    private void initView() {
        mydownload_recyclerview = (RecyclerView) findViewById(R.id.mydownload_recyclerview);
        initRecycle();
        mAdapter = new MyDownloadAdapter(this,mDownloadFiles);
        mydownload_recyclerview.setAdapter(mAdapter);

        //设置toolbar支持actionbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitle("我的下载");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    private void initData() {
//        showfilename_textview = (TextView) findViewById(R.id.showfilename_textview);
        boolean sdCardMounted = SDCardHelper.isSDCardMounted();
        //download目录路径
        String filepath = SDCardHelper.getSDCardPublicDir(Environment.DIRECTORY_DOWNLOADS);
        //download目录路径
        File path = new File(SDCardHelper.getSDCardPublicDir(Environment.DIRECTORY_DOWNLOADS));
        if (sdCardMounted&&Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            //读取文件夹下文件
            File[] files = path.listFiles();
            int length = files.length;
            getFileName(files);
//            showfilename_textview.setText(getFileName(files));

        }

    }

    private String getFileName(File[] files) {
        String str = "";
        mDownloadFiles = new ArrayList<>();
        if (files != null) {	// 先判断目录是否为空，否则会报空指针
            for (File file : files) {
                if (file.isDirectory()){//检查此路径名的文件是否是一个目录(文件夹)
                    Log.i("zeng", "若是文件目录。继续读1"
                            +file.getName().toString()+file.getPath().toString());
                    getFileName(file.listFiles());
                    Log.i("zeng", "若是文件目录。继续读2"
                            +file.getName().toString()+ file.getPath().toString());
                } else {
                    String fileName = file.getName();
                    if (fileName.endsWith(".jpg")) {
                        String s=fileName.substring(0,fileName.lastIndexOf(".")).toString();
                        mDownloadFiles.add(file);
                        Log.i("zeng", "文件名jpg：：   " + s);
                        str += fileName.substring(0,fileName.lastIndexOf("."))+"\n";
                    }
                }
            }

        }
        return str;
    }

    private void initRecycle() {
        //设置recycleView
        mydownload_recyclerview.setHasFixedSize(true);
        mydownload_recyclerview.setItemAnimator(new DefaultItemAnimator());
        mydownload_recyclerview.addItemDecoration(new DividerItemDecoration(mContext,
                DividerItemDecoration.VERTICAL_LIST));
        //设置布局管理器
        GridLayoutManager gridLayoutManager=new GridLayoutManager(mContext,2);
        mydownload_recyclerview.setLayoutManager(gridLayoutManager);
    }
}
